package com.tulink.yichutu.biz;

import com.mxixm.fastboot.weixin.exception.WxApiResultException;
import com.mxixm.fastboot.weixin.module.message.WxUserMessage;
import com.mxixm.fastboot.weixin.service.WxApiService;
import com.mxixm.fastboot.weixin.web.WxUserManager;
import com.mxixm.fastboot.weixin.web.WxWebUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 微信业务方法
 *
 * @author wanchaopeng
 */
@Slf4j
@Component
public class WxBiz {
    @Autowired
    private WxApiService wxApiService;
    @Autowired
    private WxUserManager wxUserManager;

    public String getOpenId(String code) {
        if(StringUtils.isBlank(code)) {
            return null;
        }
        try {
            WxWebUser wxWebUser = wxUserManager.getWxWebUser(code);
            if(wxWebUser != null) {
                return wxWebUser.getOpenId();
            }
        } catch (Exception ex) {
            log.error("getOpenId", ex);
        }
        return null;
    }

    public void sendUserMessage(WxUserMessage message) {
        try {
            wxApiService.sendUserMessage(message);
        } catch (WxApiResultException ex) {
            log.error("sendUserMessage: {}", ex.getMessage());
        } catch (Exception ex) {
            log.error("sendUserMessage", ex);
        }
    }
}
